package lockDemo;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Ajie
 * @date 8/23/21
 * @function
 */
public class ReentrantReadWriteLock_DeadLock_Demo {

    public static void main(String[] args) {
        MyCache myCache = new MyCache();
        new Thread(new Runnable() {
            @Override
            public void run() {
                myCache.get(0 + "");
            }
        }).start();

        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 5; i++) {
            final int temp = i;
            Thread threadWrite = new Thread(() -> {
                myCache.put(temp + "", temp + "");
            }, "write Thread "+String.valueOf(i));
            threadWrite.start();
        }
        for (int i = 0; i < 5; i++) {
            final int temp = i;
            new Thread(() -> {
                myCache.get(temp + "");
            }, "read Thread "+String.valueOf(i)).start();
        }

    }
}




